unit formUtils;

interface

uses
  Vcl.Forms, Vcl.Controls;

Function ShowForm(const FormName:String):TForm;
Function ShowFormEx(const FormName:String; ParentControl:TWinControl):TForm;

implementation

uses
  System.Classes, System.SysUtils;
// -----------------------------------------------------------------------------
Function CreateForm( const FormName : string): TForm;
var
  fc : TFormClass;
begin
  Result := nil;
  fc := TFormClass(FindClass(FormName));
  Result := fc.Create(Application);
end;

Function FindForm(const FormName:String):TForm;
var
  I: Integer;
begin
  Result := nil;
  for I := 0 to Screen.FormCount - 1 do
  begin
    if SameText(Screen.Forms[i].ClassName, FormName) then
    begin
      Result := Screen.Forms[i];
      break;
    end;
  end;
end;
// -----------------------------------------------------------------------------
Function ShowForm(const FormName:String):TForm;
var
  f: TForm;
begin
  f := FindForm(FormName);
  if Assigned(f) then
  begin
    Result := f;
    f.Show;
    Exit;
  end;

  f := CreateForm(FormName);
  f.Show;
  Result := f;
end;

Function ShowFormEx(const FormName:String; ParentControl:TWinControl):TForm;
var
  f: TForm;
begin
  Result := nil;
  f := FindForm(FormName);
  if Assigned(f) then
  begin
    f.Show;
    Result := f;
    Exit;
  end;

  f := CreateForm(FormName);
  f.Parent := ParentControl;
  f.Show;
  result := f;
end;



end.
